﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
//using System.Xml.Linq;
using System.Collections.Generic;

using YouHP.Model;
using YouHP.BLL;
using YouHP.Utility;

namespace web.admin
{
    public partial class DiseaseTreatmentsEdit : System.Web.UI.Page
    {
        private DiseaseBLL diseaseBll = DiseaseBLL.Instance;
        private TreatmentBLL treatmentBll = TreatmentBLL.Instance;
        private string URL_BACK_TEMPLATE = "DiseaseTreatments.aspx?id=";
        private int diseaseId = -1;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind();
            }
        }

        protected void Bind()
        {
            try
            {
                diseaseId = RequestTools.GetQueryInt("id", -1);
                if (diseaseId > 0)
                {
                    List<TreatmentInfo> list = treatmentBll.GetAllTreatmentByName("");
                    this.diseaseTreatmentList.DataSource = list;
                    this.diseaseTreatmentList.DataBind();
                }

                this.ViewState["diseaseid"] = "" + diseaseId;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void DataBound(object sender, EventArgs e)
        {
            try
            {
                if (diseaseId > 0)
                {
                    List<DiseaseTreatmentInfo> list = diseaseBll.GetDiseaseTreatmentList(diseaseId);
                    for (int i = 0; i < this.diseaseTreatmentList.Rows.Count; i++)
                    {
                        foreach (DiseaseTreatmentInfo dsInfo in list)
                        {
                            if (dsInfo.TreatmentName.Equals(((Label)this.diseaseTreatmentList.Rows[i].FindControl("TreatmentName")).Text))
                            {
                                ((CheckBox)this.diseaseTreatmentList.Rows[i].FindControl("IsBind")).Checked = true;
                            }
                        }
                    }

                    this.TitleName.Text = diseaseBll.GetDiseaseInfo(diseaseId).Name + "：相关治疗方案";
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void UpdateButton_click(object sender, EventArgs e)
        {
            //数据有效性检查
            try
            {
                List<DiseaseTreatmentInfo> list = new List<DiseaseTreatmentInfo>();
                for (int i = 0; i < this.diseaseTreatmentList.Rows.Count; i++)
                {
                    if (((CheckBox)this.diseaseTreatmentList.Rows[i].FindControl("IsBind")).Checked)
                    {
                        DiseaseTreatmentInfo dsInfo = new DiseaseTreatmentInfo();
                        dsInfo.TreatmentId = Convert.ToInt32(((Label)this.diseaseTreatmentList.Rows[i].FindControl("TreatmentId")).Text);

                        list.Add(dsInfo);
                    }
                }

                diseaseId = Convert.ToInt32(this.ViewState["diseaseid"].ToString());
                if (diseaseBll.UpdateDiseaseTreatment(diseaseId, list))
                {
                    HttpContext.Current.Response.Redirect(URL_BACK_TEMPLATE + diseaseId, true); //成功
                }
                else { /*失败处理*/}
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void CancelButton_click(object sender, EventArgs e)
        {
            try
            {
                HttpContext.Current.Response.Redirect(URL_BACK_TEMPLATE + Convert.ToInt32(this.ViewState["diseaseid"].ToString()), true);
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }
    }
}
